<?php

namespace App\Admin\Controllers;

use App\Models\Engineering;
use App\Models\ZiliaoContent;
use App\Models\ZiliaoCategory;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Card;
use Illuminate\Support\Facades\DB;

class ZiliaoWarehouseController extends AdminController
{
    protected $title = '资料仓库';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new ZiliaoContent(), function (Grid $grid) {
            $grid->disableViewButton();
            $grid->perPages([10, 20, 30, 40, 50,100,200,300,400,500]);

            $grid->model()->where('status', 5);


            $grid->column('id')->sortable();
            $grid->column('title','资料名称');
            $grid->column('categories_id','资料分类');
            $grid->column('engineering','所属工程');

//            $grid->column('content');
//            $grid->column('attachment');

            $grid->column('status_info','流程详情')
                ->display('流程详情') // 设置按钮名称
                ->modal(function ($modal) {

                    $status_info = json_decode($this->status_info,true);
                    $status_info_view = "";
                    foreach ($status_info as $key => $value){
//                        var_dump($value['type']);
                        $username = DB::table('admin_users')->where('id',$value['process'])->value('username');
                        if($value['status'] == 0){
                            $status = "<span style='color:red'>尚未通过</span>";
                        }else{
                            $status = "<span style='color:green'>报审通过</span>";
                        }
                        if($value['method'] == 1){
                            $method = "<span style='color:darkgoldenrod'>串行</span>";
                        }else{
                            $method = "<span style='color:#a71d5d'>并行</span>";
                        }
                        $status_info_view .= '处理人员:'."<span style='color:blue'>$username</span>".'--报审状态:'.$status.'--报审时间:'.$value['time'].'--报审方式:'.$method.'<br><br>';
                    }
                    $card = new Card(null, $status_info_view);
                    return "<div style='padding:10px 10px 0'>$card</div>";
                });

            $grid->column('created_at','建立时间');

            $grid->column('uploader','上报者')->display(function ($uploader) {
                return DB::table('admin_users')->where('id',$uploader)->value('username');
            });

            $grid->column('reviewer','审核者')->display(function ($uploader) {
                return DB::table('admin_users')->where('id',$uploader)->value('username');
            });

//            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new ZiliaoContent(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('categories_id');
            $show->field('engineering');
            $show->field('content');
            $show->field('attachment');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new ZiliaoContent(), function (Form $form) {
//            $form->display('id');
            $form->text('title','资料名称');
            $form->select('categories_id','资料分类')->options(function ($id) {
                return ZiliaoCategory::where('title','<>','')->pluck('title', 'id');
            });
            $form->select('engineering','所属工程')->options(function ($id) {
                return Engineering::where('engineering_name','<>','')->pluck('engineering_name', 'engineering_name');
            });

            $form->editor('content','内容');
            $form->file('attachment','附件')->downloadable();

            $form->hidden('status','状态');


            if (Admin::user()->can('inspector')) {
                $form->radio('status','上报者')->options(['1' => '未送审', '2'=> '提交审核'])->default('1');
            }

            if (Admin::user()->can('project_manager')) {
                $form->radio('status','审核者')->options(['1' => '驳回', '2'=> '正在审核', '3'=> '审核通过'])->default('1');
            }


            $form->hidden('uploader','上报者');

            $form->display('created_at');
            $form->display('updated_at');


            $form->saving(function (Form $form) {
                if ($form->isCreating()) {
                    $Adminid  = Admin::user()->id;
                    $form->uploader = $Adminid;
                };
            });

        });




    }
}
